<template>
    <div class="login-container">
        <div class="login-box">
            <div class="logo">
                <img src="../../assets/driving-school-logo.png" alt="驾校Logo">
                <h1>{{ schoolName }}系统</h1>
            </div>

            <el-form
                :model="loginForm"
                :rules="loginRules"
                ref="loginForm"
                class="login-form"
                @submit.native.prevent="handleLogin"
            >
                <el-form-item prop="username">
                    <el-input
                        v-model="loginForm.username"
                        placeholder="请输入账号"
                        prefix-icon="el-icon-user"

                    ></el-input>
                </el-form-item>

                <el-form-item prop="password">
                    <el-input
                        v-model="loginForm.password"
                        type="password"
                        placeholder="请输入密码"
                        prefix-icon="el-icon-lock"
                        show-password

                    ></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button
                        type="primary"
                        class="login-btn"
                        :loading="loading"
                        native-type="submit"
                    >
                        登录
                    </el-button>
                </el-form-item>

                <div class="login-footer">
                    <el-checkbox v-model="rememberMe">记住账号</el-checkbox>
                    <div class="links">
                        <a href="javascript:void(0)" @click="showNotice">忘记密码?</a>
                        <a href="javascript:void(0)" @click="showNotice">立即注册</a>
                    </div>
                </div>
            </el-form>
        </div>

        <!-- 提示对话框 -->
        <transition name="dialog-fade">
            <div v-if="noticeDialogVisible" class="simple-dialog">
                <div class="simple-dialog-content">
                    <div class="dialog-header">
                        <span class="dialog-title">提示</span>
                        <button class="simple-dialog-close" @click="noticeDialogVisible = false">✕</button>
                    </div>
                    <div class="simple-dialog-body">
                        <p class="notice-content">该功能未完善，请与管理员联系</p>
                        <div class="dialog-actions">
                            <el-button type="primary" @click="noticeDialogVisible = false">确定</el-button>
                        </div>
                    </div>
                </div>
            </div>
        </transition>
    </div>
</template>

<script>
import requestUtil from "@/util/request";

export default {
    name: 'Login',
    data() {
        return {
            schoolName: '龙王驾校',
            loginForm: {
                username: '',
                password: ''
            },
            loginRules: {
                username: [
                    {required: true, message: '请输入学员账号', trigger: 'blur'}
                ],
                password: [
                    {required: true, message: '请输入密码', trigger: 'blur'},
                    {min: 4, max: 20, message: '长度在 4 到 20 个字符', trigger: 'blur'}
                ]
            },
            rememberMe: false,
            loading: false,
            noticeDialogVisible: false
        }
    },
    created() {
        // 检查本地存储中是否有记住的账号
        const rememberedUsername = localStorage.getItem('rememberedUsername')
        if (rememberedUsername) {
            this.loginForm.username = rememberedUsername
            this.rememberMe = true
        }
        const public_key = localStorage.getItem('public_key')
        if (!public_key) {
            this.get_public_key()
        }

    },
    methods: {
        async get_public_key() {

            const response = await requestUtil.get('/login/getkey')

            console.log("从后端获取的原始公钥数据:", response)
            console.log("数据类型:", typeof response)

            let publicKey = ''

            // 处理 Axios 响应对象
            // 提取公钥数据
    if (response && response.data) {
        if (typeof response.data === 'object' && response.data.public_key) {
            publicKey = response.data.public_key
        } else if (typeof response.data === 'string') {
            publicKey = response.data
        }
    }
            console.log(publicKey)
// 验证提取到的公钥
            if (typeof publicKey === 'string' &&
                publicKey.length > 0 &&
                publicKey.includes('-----BEGIN PUBLIC KEY-----')) {
                console.log("处理后的公钥:", publicKey)
                localStorage.setItem('public_key', publicKey)
                return publicKey
            } else {
                console.error("无法提取有效的 PEM 公钥，当前提取的内容:", publicKey)
                throw new Error("无法从响应中提取有效的 PEM 公钥")
            }

        }
        ,
        async handleLogin() {
            try {
                const valid = await this.$refs.loginForm.validate()
                if (valid) {
                    this.loading = true

                    // 注意：这里假设您已经在 Vuex store 中正确配置了 login action
                    const result = await this.$store.dispatch('login/login', this.loginForm)

                    // 根据角色跳转到不同页面
                    if (result.role === 'ADMIN') {
                        this.$router.push({path: '/admin'})
                    } else if (result.role === 'COACH') {
                        this.$router.push({path: '/coach'})
                    } else if (result.role === 'STUDENT') {
                        this.$router.push({path: '/student'})
                    } else {
                        this.$message.warning('无权限')
                    }
                }
            } catch (error) {
                console.error('登录失败:', error)
                this.$message.error('登录失败，请检查用户名和密码')
            } finally {
                this.loading = false
            }
        }
        ,

        showNotice() {
            this.noticeDialogVisible = true
        }
    }
}
</script>

<style scoped>
.login-container {
    height: 100vh;
    background: linear-gradient(120deg, #184e77, #34a0a4);
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    overflow: hidden;
}

.login-container::before {
    content: "";
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0) 70%);
    transform: rotate(30deg);
}

.login-box {
    width: 100%;
    max-width: 420px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 16px;
    padding: 40px 35px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(10px);
    position: relative;
    z-index: 2;
    transform: translateY(0);
    transition: transform 0.3s ease;
}

.login-box:hover {
    transform: translateY(-5px);
}

.logo {
    text-align: center;
    margin-bottom: 35px;
}

.logo img {
    width: 90px;
    height: 90px;
    margin-bottom: 15px;
    border-radius: 50%;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.logo h1 {
    font-size: 24px;
    color: #184e77;
    margin: 0;
    font-weight: 600;
}

.login-form {
    margin-top: 10px;
}

.custom-input ::v-deep .el-input__inner {
    height: 48px;
    line-height: 48px;
    padding-left: 45px;
    border-radius: 10px;
    border: 2px solid #e2e8f0;
    font-size: 15px;
    transition: all 0.3s;
}

.custom-input ::v-deep .el-input__inner:focus {
    border-color: #34a0a4;
    box-shadow: 0 0 0 3px rgba(52, 160, 164, 0.2);
}

.custom-input ::v-deep .el-input__prefix {
    left: 15px;
    color: #718096;
}

.custom-input ::v-deep .el-input__icon {
    line-height: 48px;
    font-size: 18px;
}

.login-btn {
    width: 100%;
    height: 48px;
    margin-top: 15px;
    background: linear-gradient(120deg, #184e77, #34a0a4);
    border: none;
    border-radius: 10px;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: 1px;
    transition: all 0.3s;
}

.login-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 7px 15px rgba(52, 160, 164, 0.4);
}

.login-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 25px;
    padding-top: 20px;
    border-top: 1px solid #edf2f7;
}

.links a {
    margin-left: 15px;
    color: #4a5568;
    text-decoration: none;
    font-size: 14px;
    transition: all 0.3s;
    position: relative;
}

.links a:hover {
    color: #184e77;
}

.links a::after {
    content: "";
    position: absolute;
    bottom: -2px;
    left: 0;
    width: 0;
    height: 1px;
    background: #184e77;
    transition: width 0.3s;
}

.links a:hover::after {
    width: 100%;
}

.simple-dialog {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 9999;
}

.dialog-fade-enter-active {
    transition: all 0.3s ease;
}

.dialog-fade-leave-active {
    transition: all 0.3s ease;
}

.dialog-fade-enter, .dialog-fade-leave-to {
    opacity: 0;
}

.dialog-fade-enter .simple-dialog-content,
.dialog-fade-leave-to .simple-dialog-content {
    transform: translateY(-30px);
}

.simple-dialog-content {
    background: white;
    width: 90%;
    max-width: 500px;
    border-radius: 16px;
    padding: 0;
    position: relative;
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.25);
    transform: translateY(0);
    transition: transform 0.3s ease;
}

.dialog-header {
    padding: 20px 25px;
    border-bottom: 1px solid #edf2f7;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.dialog-title {
    font-size: 20px;
    font-weight: 600;
    color: #184e77;
}

.simple-dialog-close {
    font-size: 22px;
    background: none;
    border: none;
    cursor: pointer;
    color: #718096;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s;
}

.simple-dialog-close:hover {
    background: #edf2f7;
    color: #184e77;
}

.simple-dialog-body {
    padding: 25px;
}

.notice-content {
    font-size: 16px;
    color: #333;
    text-align: center;
    margin: 20px 0;
}

.dialog-actions {
    text-align: center;
    margin-top: 20px;
}

.dialog-actions .el-button {
    width: 100px;
}

/* 响应式设计 */
@media (max-width: 576px) {
    .login-box {
        margin: 0 20px;
        padding: 30px 20px;
    }

    .logo img {
        width: 70px;
        height: 70px;
    }

    .logo h1 {
        font-size: 20px;
    }

    .custom-input ::v-deep .el-input__inner {
        height: 44px;
        line-height: 44px;
    }

    .login-btn {
        height: 44px;
    }

    .login-footer {
        flex-direction: column;
        gap: 15px;
        align-items: flex-start;
    }

    .links {
        align-self: flex-end;
    }
}
</style>
